<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

class Utf8 {
	
	/**
	 * Checks if the given string is encoded
	 * in UTF-8.
	 * 
	 * @param $s the string to check its encoding
	 * @return true if string is encoded in UTF-8,
	 * 		false otherwise
	 */
	static function is_utf8($s)
	{
		// From http://w3.org/International/questions/qa-forms-utf-8.html
		return preg_match('%^(?:
			 [\x09\x0A\x0D\x20-\x7E]			# ASCII
		   | [\xC2-\xDF][\x80-\xBF]				# non-overlong 2-byte
		   |  \xE0[\xA0-\xBF][\x80-\xBF]		# excluding overlongs
		   | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}	# straight 3-byte
		   |  \xED[\x80-\x9F][\x80-\xBF]		# excluding surrogates
		   |  \xF0[\x90-\xBF][\x80-\xBF]{2}		# planes 1-3
		   | [\xF1-\xF3][\x80-\xBF]{3}			# planes 4-15
		   |  \xF4[\x80-\x8F][\x80-\xBF]{2}		# plane 16
		)*$%xs', $s);
	}
}
	
?>